package com.zw.pdm.module.est.service;

import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.est.CardChange;
import com.zw.pdm.entity.est.CardDetail;
import com.zw.pdm.module.designtask.repository.DsnProStructRepository;
import com.zw.pdm.module.est.mapper.CardDetailMapper;
import com.zw.pdm.module.est.mapper.CardMapper;
import com.zw.pdm.module.est.repository.CardChangeRepository;
import com.zw.pdm.module.est.repository.CardDetailChangeRepository;
import com.zw.pdm.module.est.repository.CardDetailRepository;
import com.zw.pdm.module.est.repository.CardRepository;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.CommonUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zw.pdm.util.BeanUtil.copy;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class CardListService {

    @Autowired
    private CardRepository cardRepository;
    @Autowired
    private CardChangeRepository cardChangeRepository;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardDetailRepository cardDetailRepository;
    @Autowired
    private CardDetailChangeRepository cardDetailChangeRepository;
    @Autowired
    private CardDetailMapper cardDetailMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private DsnProStructRepository dsnProStructRepository;

    /** 列表 */
    public List<Card> findWithCardList(Card card) {
        card.setCreateCodeOne(ShiroUtils.getLoginName());
        return cardMapper.findWithCardList(card);
    }

    /** 按主键查询 */
    public Card getCardById(Integer id) {
        return cardRepository.getOne(id);
    }

    /** 删除工艺卡 */
    @Transactional(rollbackFor = Exception.class)
    public int delCardById(Integer id) {
        logService.saveLog("删除工艺卡", "工艺卡ID：" + id);
        cardMapper.updateIsDelById(id, 1);
        return cardDetailMapper.updateIsDelByCardId(id, 1);
    }

    /** 提交工艺卡 */
    public int commitCardById(Integer id) {
        logService.saveLog("提交工艺卡", "工艺卡ID：" + id);
        return cardMapper.updateStatusById(id, 2);
        //return cardMapper.updateStatusAndRejectReasonById(id, 2, null);
    }

    /** 批量提交工艺卡 */
    public int commitCardByIds(Integer[] ids) {
        logService.saveLog("提交工艺卡", "批量");
        return cardMapper.updateStatusAndRejectByIds(ids, 2, null);
    }

    /** 查询工艺卡明细 */
    public List<CardDetail> findCardDetailByCardId(Integer id) {
        return cardDetailMapper.findByCardId(id);
    }

    /** 删除工艺卡明细 */
    public int delDetailById(Integer id) {
        return cardDetailMapper.updateIsDelById(id, 1);
    }

    /** 修改工艺卡 */
    @Transactional(rollbackFor = Exception.class)
    public int update(Card card, Integer[] detailId, String[] route, Integer[] orderNo, String[] process, String[] equName, String[] craftContent) {
        DsnProStruct dsnProStruct = dsnProStructRepository.getOne(card.getPartId());
        card.setPart(dsnProStruct.getProPart());
        Card target = cardRepository.getOne(card.getId());
        //CardChange cc = saveChangeCard(card, target);
        copy(card, target);
        target.setSpec(card.getSpec());
        cardRepository.save(target);
        for (int i = 0; i < route.length; i++) {
            CardDetail detail = new CardDetail();
            //CardDetailChange cdc = new CardDetailChange();
            //cdc.setCardChangeId(cc.getId());
            //cdc.setCreateTime(new Date());
            //cdc.setIsDel(0);
            if (detailId.length > 0) {
                if (detailId[i] == null) {
                    detail.setCardId(target.getId());
                    detail.setCreateTime(new Date());
                    detail.setMayChange(0);
                    detail.setIsDel(0);
                } else {
                    detail = cardDetailRepository.getOne(detailId[i]);
                }
            } else {
                detail.setCardId(target.getId());
                detail.setCreateTime(new Date());
                detail.setMayChange(0);
                detail.setIsDel(0);
            }

            //cdc.setRoute(detail.getRoute());
            //cdc.setOrderNo(detail.getOrderNo());
            //cdc.setProcess(detail.getProcess());
            //cdc.setEquName(detail.getEquName());
            //cdc.setCraftContent(detail.getCraftContent());
            //cdc.setRouteCode(detail.getRouteCode());
            //
            //cdc.setNewRouteCode(null);
            //cdc.setNewRoute(route[i]);
            //cdc.setNewOrderNo(orderNo[i]);
            //cdc.setNewProcess(process[i]);
            //cdc.setNewCraftContent(CommonUtils.handlerEnterAndSpace(craftContent[i]));

            detail.setOrderNum(i);
            detail.setRoute(route[i]);
            detail.setOrderNo(orderNo[i]);
            detail.setProcess(process[i]);
            if (equName.length > 0) {
                detail.setEquName(equName[i]);
                //cdc.setNewEquName(equName[i]);
            } else {
                detail.setEquName("");
                //cdc.setNewEquName("");
            }
            detail.setCraftContent(CommonUtils.handlerEnterAndSpace(craftContent[i]));
            detail = cardDetailRepository.save(detail);
            //cardDetailChangeRepository.save(cdc);
        }
        logService.saveLog("修改工艺卡", "工艺卡ID：" + card.getId());
        return 1;
    }

    /** 保存变更信息 */
    private CardChange saveChangeCard(Card card, Card target) {
        CardChange cc = new CardChange();
        cc.setCardId(card.getId());
        cc.setCreateTime(new Date());
        cc.setIsDel(0);
        cc.setNewDrawNo(card.getDrawNo());
        cc.setNewDrawName(card.getDrawName());
        cc.setNewDrawNum(card.getDrawNum());
        cc.setNewQuality(card.getQuality());
        cc.setNewSpec(card.getSpec());
        cc.setNewWeight(card.getWeight());
        cc.setQuality(target.getQuality());
        cc.setSpec(target.getSpec());
        cc.setWeight(target.getWeight());
        cc.setDrawNo(target.getDrawNo());
        cc.setDrawName(target.getDrawName());
        cc.setDrawNum(target.getDrawNum());
        return cardChangeRepository.save(cc);
    }


}
